﻿using System;
using System.Collections;
using System.Collections.Generic;

namespace CSharpConsoleTestSolution.Demo {
    //表达式
    public class P16_Expression {
        /**
         * while 语句
         * do while 语句
         */


        /**
         * continue 语句, 时间: 19:00
         */
        public static void continue_exam() {
            for (int i = 0; i < 10; i++) {
                if (i == 4) {
                    continue;   //中断本次循环, 继续下次循环
                }
                if (i == 8) {
                    break;      //跳出循环
                }
                Console.WriteLine($"i = {i}");
            }
        }


        /**
         *  Enumerator 迭代器, 时间: 49:03
         */
        public static void Enumerator() {
            int[] intArray = new int[] { 1, 2, 3, 4, 5, 6 };
            Console.WriteLine($"intArray is Array: {intArray is Array}");
            
            for (int i = 0; i < intArray.Length; i++) {         //数组, 迭代方法1
                Console.Write($"{intArray[i]}, ");
            }
            Console.WriteLine();

            
            foreach (int VARIABLE in intArray) {                //数组, 迭代方法2
                Console.Write($"{VARIABLE}, ");
            }
            Console.WriteLine();
            
            
            IEnumerator enumerator = intArray.GetEnumerator();  //枚举器, 计数器
            while (enumerator.MoveNext()) {                     //数组, 迭代方法3
                Console.Write($"{enumerator.Current}, ");
            }
            Console.WriteLine();

            
            enumerator.Reset();                                 //重置
            for (; enumerator.MoveNext();) {                    //数组, 迭代方法4
                Console.Write($"{enumerator.Current}, ");
            }
            Console.WriteLine();





            //List
            List<int> intList = new List<int>() { 7, 8, 9, 10, 11, 12 };
            for (int i = 0; i < intList.Count; i++) {           //List, 迭代方法1
                Console.Write($"{intList[i]}, ");
            }
            Console.WriteLine();


            foreach (int VARIABLE in intList) {                 //List, 迭代方法2
                Console.Write($"{VARIABLE}, ");
            }
            Console.WriteLine();
            
            
            List<int>.Enumerator enumerator1 = intList.GetEnumerator(); //枚举器, 计数器
            while (enumerator1.MoveNext()) {                    //List, 迭代方法3
                Console.Write($"{enumerator1.Current}, ");
            }
            Console.WriteLine();
            
            //释放由枚举器（Enumerator）使用的所有资源。
            enumerator1.Dispose();
            // Reset() 方法不是 public ...
            // enumerator1.Reset(); 
            
            
            List<int>.Enumerator enumerator2 = intList.GetEnumerator(); //枚举器, 计数器
            for (; enumerator2.MoveNext();) {                   //List, 迭代方法4
                Console.Write($"{enumerator2.Current}, ");
            }
            Console.WriteLine();
            enumerator2.Dispose();
        }
    }
}